package com.ruoyi.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inventory.domain.DeliveryDetail;
import com.ruoyi.inventory.domain.vo.QueryDeliverVo;
import com.ruoyi.inventory.domain.vo.QueryProductVo;
import com.ruoyi.inventory.service.IDeliveryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.inventory.mapper.DeliveryMapper;
import com.ruoyi.inventory.domain.Delivery;
import com.ruoyi.inventory.service.IDeliveryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 配货单Service业务层处理
 *
 * @author xwj
 * @date 2024-11-10
 */
@Service
public class DeliveryServiceImpl extends ServiceImpl<DeliveryMapper, Delivery> implements IDeliveryService {
    @Autowired
    private DeliveryMapper deliveryMapper;
    @Autowired
    private IDeliveryDetailService deliveryDetailService;

    /**
     * 查询配货单
     *
     * @param id 配货单主键
     * @return 配货单
     */
    @Override
    public Delivery selectDeliveryById(Long id) {
        Delivery delivery = deliveryMapper.selectDeliveryById(id);

        DeliveryDetail query = new DeliveryDetail();
        query.setDeliveryId(id);
        List<DeliveryDetail> detailList = deliveryDetailService.selectDeliveryDetailList(query);
        delivery.setDetailList(detailList);

        return delivery;
    }

    /**
     * 查询配货单列表
     *
     * @param delivery 配货单
     * @return 配货单
     */
    @Override
    public List<Delivery> selectDeliveryList(Delivery delivery) {
        return deliveryMapper.selectDeliveryList(delivery);
    }

    /**
     * 新增配货单
     *
     * @param delivery 配货单
     * @return 结果
     */
    @Override
    @Transactional
    public Delivery insertDelivery(Delivery delivery) {
        save(delivery);
        insertDeliveryDetail(delivery);
        return delivery;
    }

    /**
     * 修改配货单
     *
     * @param delivery 配货单
     * @return 结果
     */
    @Override
    @Transactional
    public Delivery updateDelivery(Delivery delivery) {
        insertDeliveryDetail(delivery);
        updateById(delivery);
        return delivery;
    }

    // 插入详情
    private void insertDeliveryDetail(Delivery delivery) {
        List<DeliveryDetail> details = delivery.getDetailList();
        Long id = delivery.getId();;
        Date now = new Date();

        // 清空子表
        Long[] ids = {id};
        deliveryDetailService.deleteDeliveryDetailByDeliveryIds(ids);

        if(StringUtils.isNotNull(details)) {
            for(DeliveryDetail detail : details) {
                detail.setDeliveryId(id);
                detail.setCreateTime(now);
                deliveryDetailService.insertDeliveryDetail(detail);
            }
        }
    }

    /**
     * 批量删除配货单
     *
     * @param ids 需要删除的配货单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDeliveryByIds(Long[] ids) {
        deliveryDetailService.deleteDeliveryDetailByDeliveryIds(ids);
        int num = deliveryMapper.deleteDeliveryByIds(ids);

        return num;
    }

    /**
     * 条件删除
     *
     * @param delivery 配货单
     * @return
     */
    @Override
    public int delete(Delivery delivery) {
        QueryWrapper<Delivery> qw = new QueryWrapper<>();
        QueryTool.condition(qw, delivery);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     *
     * @param delivery 配货单
     * @return
     */
    @Override
    public Pages<Delivery> queryPage(Delivery delivery) {
        QueryWrapper<Delivery> qw = new QueryWrapper<>();
        QueryTool.condition(qw, delivery);
        IPage<Delivery> ipage = new Query<Delivery>().getPage(delivery);

        IPage<Delivery> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    /**
     * 删除配货单信息
     *
     * @param id 配货单主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryById(Long id) {
        Long[] ids = {id};
        deliveryDetailService.deleteDeliveryDetailByDeliveryIds(ids);
        return deliveryMapper.deleteDeliveryById(id);
    }

    /**
     * 统计 本月供货单数
     * @return
     */
    @Override
    public List<Map> statistics(String year, Long customerId) {
        return deliveryMapper.statistics(year, customerId);
    }

    @Override
    public List<Map> report(QueryDeliverVo queryDeliverVo) {
        return deliveryMapper.report(queryDeliverVo);
    }

    @Override
    public List<Map> reportDay(QueryDeliverVo queryDeliverVo) {
        return deliveryMapper.reportDay(queryDeliverVo);
    }

    @Override
    public List<Map> reportCustomer(String year, Long customerId) {
        return deliveryMapper.reportCustomer(year, customerId);
    }

    @Override
    public List<Map> reportCustomerDay(String startTime, String endTime, Long customerId) {
        return deliveryMapper.reportCustomerDay(startTime, endTime, customerId);
    }

    @Override
    public List<Map> reportCost(QueryDeliverVo queryDeliverVo) {
        return deliveryMapper.reportCost(queryDeliverVo);
    }

    @Override
    public List<Map> reportGood(QueryDeliverVo queryDeliverVo) {
        return deliveryMapper.reportGood(queryDeliverVo);
    }

    @Override
    public List<Map> generateShopList(QueryProductVo queryProductVo) {
        return deliveryMapper.generateShopList(queryProductVo);
    }

    @Override
    public List<Map> reportBill(QueryDeliverVo queryDeliverVo) {
        return deliveryMapper.reportBill(queryDeliverVo);
    }

    @Override
    public List<Map> reportPrice(QueryDeliverVo queryDeliverVo) {
        return deliveryMapper.reportPrice(queryDeliverVo);
    }
}
